<?php
/**
 * 单开转多开版数据同步
 */
namespace App;
defined('IN_IA') or exit('Access Denied');

use Plug\Plug;

class MultiSync extends Action
{
    private $MultiSyncPlug = null;

    public function __construct()
    {
        parent::__construct();
        $Plug = new Plug(Plug::MULTI_SYNC);
        if($Plug->check()){
            $this->MultiSyncPlug = $Plug->get();
        }
    }

    /**
     * 检测是否有单开版的的数据
     */
    public function multiSyncTest()
    {
        if($this->MultiSyncPlug->multiSyncTest()){
            $this->_result = array("status"=>"success","data"=>true);
        }
        $this->result($this->_result);
    }

    /**
     * 获取同步的uniacid
     */
    public function getSyncUniacid()
    {
        if(!$_uniacid = $this->MultiSyncPlug->getSyncUniacid()){
           $_uniacid = 0;
        }
        $this->_result = array("status"=>"success","data"=>$_uniacid);
        $this->result($this->_result);
    }

    /**
     * 获取需要同步的数据表
     */
    public function getSyncTables()
    {
        $this->_result = array("status"=>"success","data"=>$this->MultiSyncPlug->getSyncTables());
        $this->result($this->_result);
    }

    /**
     * 获取同步数据数量
     */
    public function getSyncNumber()
    {
        $this->_result = array("status"=>"success","data"=>$this->MultiSyncPlug->getSyncNumber());
        $this->result($this->_result);
    }

    /**
     * 获取一个需要同步的数据的id
     * @param string table_name 数据库名字
     */
    public function get()
    {
        if($_data = pdo_get($this->_gpc["table_name"],array("uniacid"=>0),array("id"))){
            $this->_result = array("status"=>"success","data"=>$_data["id"]);
        }
        $this->result($this->_result);
    }

    /**
     * 同步数据
     * @param string table_name 表明
     * @param int sync_id 同步的id
     */
    public function sync()
    {
        if(pdo_update($this->_gpc["table_name"],array("uniacid"=>$this->_uniacid),array("id"=>$this->_gpc["sync_id"]))){
            $this->_result = array("status"=>"success");
        }
        $this->result($this->_result);
    }
}